package maldi

import protein.Protein

class ProteinController {

    def index() { 
        
    //println "here"
    }    

    
    def list() {
        //println "in list"
        //println params
        //def geneName = params?.geneName
        //def ipiAccession = params?.ipiAccession
        def minMass = 0
        def maxMass = 0
        def where = ''
        def from = ''
        
        if((params?.geneName!=null) && (params?.geneName!='')) {
            where = where+' and n.name=\''+params?.geneName+'\''
            from = ' join p.genes n'
        }else {
            from = ' join p.genes n'
        }
        
        if((params?.ipiAccession!=null) && (params?.ipiAccession!='') ) {
            where = where+' and p.mainId=\''+params?.ipiAccession+'\''
        }
        if((params?.minMass!=null)  && (params?.minMass!='')) {
           minMass = params?.minMass.toInteger() 
           where = where+' '
        }
        if((params?.range!=null) && (params?.range!='')) {
            minMass = params?.minMass.toInteger() - params?.range.toInteger()
            maxMass = params?.minMass.toInteger() + params?.range.toInteger()
            where = where+' and g.calculatedPeptideMass<'+maxMass
        }
        
        //println where        
        def result = []
        def temp = ''
      
        if(where.size()!=0){
            def bla = 'select p.mainId, g.peptideSeq, g.calculatedPeptideMass, g.sampleCategory, g.biologicalSampleName, g.molecularWeight, g.peptideStartIndex, g.peptideStopIndex, p.id, g.project, n.name from Protein p join p.proteinGroup g'+from+' where g.calculatedPeptideMass>'+minMass+' '+where
            //def bla = 'select p.mainId, g.peptideSeq, g.calculatedPeptideMass from Protein p, ProteinGroups g'+from+' where g.protein=p.proteinGroup'
            //println bla
            Protein.executeQuery(bla).each{line->
               def ipiAc = line[0]
               def pepSeq = line[1]
               def pepMass = line[2]
               def sampleCat = line[3]
               def bioSample = line[4]
               def molWeight = line[5]
               def startIndex = line[6]
               def stopIndex = line[7]
               def proteinId = line[8]
               def prjName = line[9]
               def genes = line[10]
               
               temp = 'select count(l.publication) from Literature l where protein.id=\''+proteinId+'\''
               def cc =  Protein.executeQuery(temp)
               
               println line + cc
               //println minMass+" range => "+ maxMass+"ipi -> "+ params?.ipiAccession
               //println project
               result.add([mainId:ipiAc, peptideSeq:pepSeq, calculatedPeptideMass:pepMass, sampleCategory:sampleCat,
                   biologicalSampleName:bioSample, molecularWeight:molWeight, peptideStartIndex:startIndex, peptideStopIndex:stopIndex, proteinId:proteinId, project:prjName, name:genes, cc:cc])                
            }
        }
        
        render(template: "list", model: [result:result])
    }
    
    def literature() {
        //println params?.id
        def bla = 'select l.publication, l.swissUniprot, l.measuredMass, l.methioninLoss, l.acetylation, l.pubmedID, l.species, l.tissue, l.originalIdentifier, l.proteinName, l.otherModes from Literature l where protein.id='+params.id
        
        //println "in literature"
        def result = []
        
        Protein.executeQuery(bla).each{line->
            def publication = line[0]
            def swissUniprot = line[1]
            def measuredMass = line[2]
            def methioninLoss = line[3]
            def acetylation = line[4]
            def pubmed = line[5]
            def species = line[6]
            def tissue = line[7]
            def originalIdentifier = line[8]
            def proteinName = line[9]
            def otherModes = line[10]
           
            println line
            result.add([publication:publication, swissUniprot:swissUniprot, measuredMass:measuredMass, methioninLoss:methioninLoss, acetylation:acetylation,
                pubmedID:pubmed, species:species, tissue:tissue, originalIdentifier:originalIdentifier,  proteinName:proteinName, otherModes:otherModes])
        }
       render(template: "literature", model: [result:result]) 
       //return [result:result]
    }
    
    
    def lit() {
       // println "in litsadf fds "
        
        def where = ''
        def from = ''    
        def minMass = 0
        def maxMass = 0
        
        
        if((params?.geneName!=null) && (params?.geneName!='')) {
            where = where+' and n.name=\''+params?.geneName+'\''
            from = ' join p.genes n'
        }        
        if((params?.ipiAccession!=null) && (params?.ipiAccession!='') ) {
            where = where+' and p.mainId=\''+params?.ipiAccession+'\''
        }        
        if((params?.minMass!=null)  && (params?.minMass!='')) {
           minMass = params?.minMass.toInteger() 
           where = where+' '
        }        
        if((params?.range!=null) && (params?.range!='')) {
            minMass = params?.minMass.toInteger() - params?.range.toInteger()
            maxMass = params?.minMass.toInteger() + params?.range.toInteger()
            where = where+' and l.measuredMass<'+maxMass
        }
                
        def litQuery = 'select p.mainId, l.publication, l.swissUniprot, l.measuredMass, l.methioninLoss, l.acetylation, l.pubmedID, l.species, l.tissue, l.originalIdentifier, l.proteinName, l.otherModes from Protein p join p.literature l '+from+ ' where l.measuredMass>'+minMass+ ' ' + where
        println litQuery
        
        def result = []
        Protein.executeQuery(litQuery).each{line->
            def mainId = line[0]
            def publication = line[1]
            def swissUniprot = line[2]
            def measuredMass = line[3]
            def methioninLoss = line[4]
            def acetylation = line[5]
            def pubmed = line[6]
            def species = line[7]
            def tissue = line[8]
            def originalIdentifier = line[9]
            def proteinName = line[10]
            def otherModes = line[11]
            
            result.add([mainId:mainId, publication:publication, swissUniprot:swissUniprot, measuredMass:measuredMass, methioninLoss:methioninLoss, acetylation:acetylation,
                pubmedID:pubmed, species:species, tissue:tissue, originalIdentifier:originalIdentifier,  proteinName:proteinName, otherModes:otherModes])
            println line
            
        }
        render(template: "literatureResult", model: [result:result]) 
    }
}
